<template>
  <div>
    <el-row :gutter="20">
      <el-col :span="20" :offset="2">
        <el-tabs
          type="border-card"
          class="recipe_title"
          v-model="activeName"
          @tab-click="hadlerTab"
        >
          <el-tab-pane
            :label="v.parent_name"
            v-for="(v, i) of recipe"
            :key="i"
            :name="v.parent_type"
            ><router-link
              :to="{ query: { classify: list.type } }"
              v-for="(list, index) of v.list"
              :key="index"
            >
              <el-tag :class="{ 'el-tags': classify == list.type }">{{
                list.name
              }}</el-tag></router-link
            >
          </el-tab-pane>
        </el-tabs>
        <p class="recipe_title_p">家常好味道，给你家一般的温暖</p>
        <el-container>
          <el-aside width="280px"
            ><h5>筛选</h5>
            <el-collapse v-model="activeNames" @change="handleChange">
              <el-collapse-item
                :title="v.parent_name"
                :name="v.title"
                v-for="(v, i) of propertys"
                :key="i"
              >
                <!-- <router-link
                  :to="{
                    query: { ...$route.query, [v.title]: list.type, page: 1 },
                  }"
                  v-for="(list, index) of v.list"
                  :key="index"
                > -->
                <el-tag
                  @click="handlerPropertyClick(list.type, v.title)"
                  v-for="(list, index) of v.list"
                  :key="index"
                  :class="{ 'el-tags': $route.query[v.title] == list.type }"
                  >{{ list.name }}</el-tag
                >
                <!-- </router-link> -->
              </el-collapse-item>
            </el-collapse>
          </el-aside>
          <el-main>
            <!-- <el-row :gutter="20" justify="space-between" type="flex">
              <el-col
                class="recipe_aside"
                v-for="(v, i) of 5"
                :key="i"
                :span="8"
                :offset="0"
                >{{ i }}</el-col
              >
            </el-row> -->
            <p v-show="loading" class="recipe_loading">
              <i class="el-icon-loading"></i> 加载中
            </p>
            <menu-card
              v-show="menuList.list"
              :menuList="menuList"
              width="19%"
              margin="0 4px 20px"
            />
            <el-empty
              v-show="menuList.total == 0 && !loading"
              :image-size="200"
            ></el-empty>
            <div class="recipe_pagination">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page.sync="menuList.current_page"
                :page-size="menuList.page_size"
                layout="total, prev, pager, next"
                :total="menuList.total"
              >
              </el-pagination>
            </div>
          </el-main>
        </el-container>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { getClassify, getProperty, getMenuQuery } from "@/api";
import menuCard from "@/components/menu-card.vue";
export default {
  components: { menuCard },
  data() {
    return {
      activeName: "", //导航栏
      classify: "", //导航栏选中属性
      recipe: [], //导航栏数据
      activeNames: [], //折叠筐展开
      propertys: [], //折叠筐数据
      menuList: {}, //菜谱数据
      loading: true, //加载
    };
  },
  methods: {
    handleChange(val) {
      console.log(val);
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    // 换页
    async handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.$router.push({ query: { ...this.$route.query, page: val } });
    },
    //导航栏切换
    hadlerTab(val) {
      const recipe = this.recipe.find((i) => {
        return val.name == i.parent_type;
      });
      //切换默认选中
      this.$router.push("?classify=" + recipe.list[0].type);
    },
    //折叠筐路由跳转
    handlerPropertyClick(type, title) {
      let query = { ...this.$route.query };
      query.page = 1;
      if (query[title] == type) {
        delete query[title];
      } else {
        this.$set(query, title, type);
      }
      this.$router.push({ query });
    },
    async getMeun(val) {
      //获取菜谱数据
      this.loading = true;
      this.menuList = {};
      let property = { ...val.query };
      delete property.classify;
      delete property.page;
      const page = val.query.page || 1;
      let query = {
        classify: val.query.classify || this.classify,
        page,
        property,
      };
      this.menuList = (await getMenuQuery(query)).data;
      this.loading = false;
      console.log(query);
    },
    async funClassify() {
      //获取导航栏数据
      this.recipe = (await getClassify()).data;
      if (!this.$route.query.classify) {
        //设置默认值
        this.activeName = this.recipe[0].parent_type;
        this.classify = this.recipe[0].list[0].type;
      }
      console.log(this.recipe);
    },
    async funProperty() {
      //获取折叠筐数据
      this.propertys = (await getProperty()).data;
      //默认全部展开
      for (let i in this.propertys) {
        if (this.$route.query[this.propertys[i].title]) {
          this.activeNames.push(this.propertys[i].title);
        }
      }
      console.log(this.propertys);
    },
  },

  watch: {
    $route: {
      async handler(val) {
        const { classify } = val.query;
        if (classify) {
          //设置导航栏选中值
          this.activeName = classify.split("-")[0];
          this.classify = classify;
        }
        if (this.recipe.length == 0) await this.funClassify();
        if (this.propertys.length == 0) await this.funProperty();
        //获取菜谱数据
        this.getMeun(val);
      },
      immediate: true, //立即执行
    },
  },
};
</script>

<style lang="stylus">
.recipe_title {
  margin-top: 20px;

  .el-tabs__header {
    background-color: rgba(0, 0, 0, 0);
  }

  span {
    // padding: 8px;
    // background-color: #f1f0f0;
    // color: #827171;
    margin-right: 20px;
    cursor: pointer;
  }
}

.el-tags {
  background: red;
  color: #fff;
}

.recipe_title_p {
  line-height: 8em;
  text-align: center;
  font-size: 18px;
}

.el-aside {
  padding: 10px;
  background-color: #fff;
  margin-right: 20px;

  h5 {
    line-height: 2em;
  }

  span {
    margin-top: 20px;
    margin-right: 20px;
    cursor: pointer;
  }
}

.el-main {
  padding: 0;
}

.recipe_loading {
  text-align: center;
}

.recipe_pagination {
  float: right;
  margin-top: 20px;
}
</style>